package com.sunwayworld.basemodule.basedata.codes.service.impl;

import com.sunwayworld.basemodule.basedata.codes.service.LimsCodeService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.data.page.Page;
import com.sunwayworld.framework.exception.InvalidDataException;
import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.mybatis.page.PageRowBounds;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.module.sys.code.bean.CoreCodeBean;
import com.sunwayworld.module.sys.code.bean.CoreCodeCategoryBean;
import com.sunwayworld.module.sys.code.dao.CoreCodeDao;
import com.sunwayworld.module.sys.code.service.CoreCodeCategoryService;
import com.sunwayworld.module.sys.code.service.CoreCodeService;
import org.antlr.stringtemplate.language.Cat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@GikamBean
@Repository
public class LimsCodeServiceImpl implements LimsCodeService {
    @Autowired
    private CoreCodeDao codeDao;

    @Autowired
    @Lazy
    private CoreCodeCategoryService coreCodeCategoryService;

    @Autowired
    @Lazy
    private CoreCodeService coreCodeService;

    public LimsCodeServiceImpl() {
    }

    public CoreCodeDao getDao() {
        return this.codeDao;
    }

    @Transactional
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        CoreCodeBean code = jsonWrapper.parseUnique(CoreCodeBean.class);
        code.setId(ApplicationContextHelper.getNextIdentity());
        this.getDao().insert(code);
        return code.getId();
    }

    public Page<CoreCodeBean> selectChoosablePagination(RestJsonWrapperBean wrapper) {
        MapperParameter parameter = wrapper.extractMapFilter();
        PageRowBounds rowBounds = wrapper.extractPageRowBounds();
        return this.selectPagination(parameter, rowBounds);
    }

    @Override
    @Transactional
    public String addCategory(RestJsonWrapperBean wrapper) {
        CoreCodeCategoryBean categoryBean = wrapper.parseUnique(CoreCodeCategoryBean.class);
        categoryBean.setCodeClassId("biz");
        coreCodeCategoryService.getDao().insert(categoryBean);
        return categoryBean.getId();
    }

    @Override
    @Transactional
    public String deleteCategory(RestJsonWrapperBean wrapper) {
        List<CoreCodeCategoryBean> beans = wrapper.parse(CoreCodeCategoryBean.class);
        if (beans.isEmpty()) {
            return "";
        }
        List<String> ids = beans.stream().map(CoreCodeCategoryBean::getId).collect(Collectors.toList());

        List<CoreCodeCategoryBean> sysCodes = coreCodeCategoryService.selectListByFilter(SearchFilter.instance().match("id", ids).filter(MatchPattern.OR).match("codeClassId", "sys").filter(MatchPattern.EQ));
        if (!sysCodes.isEmpty()) {
            String msg = sysCodes.stream().map(CoreCodeCategoryBean::getCodeCategoryName).collect(Collectors.joining(",<br/>"));
            throw new InvalidDataException(msg.concat(I18nHelper.getMessage("CORE.MODULE.SYS.CODECATEGORIES.TIP.SYS_CANNOT_DELETE")));
        }

        List<CoreCodeBean> codes = coreCodeService.selectListByFilter(SearchFilter.instance().match("codeCategoryId", ids).filter(MatchPattern.OR));
        List<Long> codeIds = codes.stream().map(CoreCodeBean::getId).collect(Collectors.toList());

        coreCodeService.getDao().deleteByIdList(codeIds);
        coreCodeCategoryService.getDao().deleteByIdList(ids);
        return "";
    }
}
